package com.hyjx.framework.component.task;

import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.orm.dao.IBaseDao;
import com.hyjx.framework.orm.dao.Pager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 任务管理服务实现类
 */
@Service
@Transactional
public class TaskServiceImpl implements TaskService {

    @Resource
    private IBaseDao dao;

    @SuppressWarnings("unchecked")
    public String queryList(TaskModel model) {
        Pager pager = dao.getPager(model.getModuleName() + ".queryList", model.getModuleName() + ".queryCount", model);
        for (TaskModel result : (List<TaskModel>) pager.getResultList()) {
            String runstatus = TaskManager.getTaskStatus(result.getTaskid());
            result.setRunstatus(runstatus);
            //result.setTasktypename(translateDmToMc(result.getTasktype()));
        }
        return CommonUtil.getPageJsonStr(pager);
    }

    public String queryLogList(String taskid) {
        TaskModel model = new TaskModel();
        Pager pager = dao.getPager(model.getModuleName() + ".queryLogList", model.getModuleName() + ".queryLogCount", taskid);

        return CommonUtil.getPageJsonStr(pager);
    }

    @Override
    public boolean add(TaskModel model) {
        return dao.addObject(model.getModuleName() + ".insert", model);
    }

    @Override
    public TaskModel getInfoById(String s) {
        return  (TaskModel) dao.queryObject(new TaskModel().getModuleName() + ".getInfoById", s);
    }

    @Override
    public int edit(TaskModel model) {
        return dao.updateObject(model.getModuleName() + ".update", model);
    }

    @Override
    public int delete(String ids) {
        String[] id_array = ids.split(",");
        int num = 0;
        for (String id : id_array) {
            dao.deleteObject(new TaskModel().getModuleName() + ".delete", id);
            num++;
        }
        return num;
    }

    @Override
    public int deletelog(String id) {
        int rtn = dao.deleteObject(new TaskModel().getModuleName() + ".deletelogByTaskId", id);

        return rtn;
    }

    @Override
    public int start(String ids) {
        TaskModel model = TaskManager.getTaskById(ids);
        if (TaskManager.startTask(model)) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int stop(String ids) {
        TaskModel model = new TaskModel();
        model.setTaskid(ids);
        model = getInfoById(ids);
        int ret = StopJob(model);
        model.setStatus("0");
        if (ret > 0)
            edit(model);
        else
            return 0;
        return 1;
    }

    public int StopJob(TaskModel model) {
        if (TaskManager.stopTask(model.getTaskid())) {
            return 1;
        }
        return 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TaskModel> queryAllTask() {
        return (List<TaskModel>) dao.queryObjectList(new TaskModel().getModuleName() + ".queryAllTask");
    }

    @Override
    public void saveTaskLog(TaskLogModel model) {
        dao.addObject(new TaskModel().getModuleName() + ".insertlog", model);
    }

    @Override
    public void deletelogByTaskId(String taskid) {
        dao.deleteObject(new TaskModel().getModuleName() + ".deletelogByTaskId", taskid);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TaskLogModel> queryTaskLogByTaskId(String taskid) {
        return dao.queryObjectList(new TaskModel().getModuleName() + ".queryTaskLogByTaskId", taskid);
    }
}